package com.fdc_project.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.conditions.query.QueryChainWrapper;
import com.fdc_project.entity.Customer;
import com.fdc_project.entity.Doingproject;
import com.fdc_project.entity.Historyproject;
import com.fdc_project.mapper.DoingprojectMapper;
import com.fdc_project.service.IDoingprojectService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.fdc_project.vo.ErrorCode;
import com.fdc_project.vo.Result;
import com.sun.applet2.preloader.event.ErrorEvent;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.DependsOn;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author Sk
 * @since 2021-11-29
 */
@Service
public class DoingprojectServiceImpl extends ServiceImpl<DoingprojectMapper, Doingproject> implements IDoingprojectService {

    @Autowired
    DoingprojectMapper doingprojectMapper;

    @Autowired
    HistoryprojectServiceImpl historyprojectService;

    @Override
    public Result add(List<Doingproject> doingproject) {
        for (Doingproject doingproject1 : doingproject) {
            try {
                doingprojectMapper.insert(doingproject1);
            } catch (Exception e) {
                return Result.fail(ErrorCode.PARAMS_ERROR.getCode(), "添加失败,参数错误！");
            }
        }
        return Result.success(doingproject);
    }

    @Override
    public Result deleteAll(Integer pro_id) {
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("pro_id",pro_id);
        List<Doingproject> list =(List<Doingproject>) doingprojectMapper.selectList(queryWrapper);
        Doingproject doingproject = list.get(1);
        if(list.isEmpty() || doingproject.getDeleted() == 1){
            return Result.fail(ErrorCode.USER_NULL.getCode(), "该项目信息不存在！");
        }
        queryWrapper.clear();
        for (Doingproject doingproject1 : list) {                                  //遍历表中所有信息并逻辑删除
            queryWrapper.eq("pro_id",doingproject1.getProId());
            queryWrapper.eq("staff_id",doingproject1.getStaffId());
            doingproject1.setDeleted(1);
            doingprojectMapper.update(doingproject1,queryWrapper);
            historyprojectService.autoAddHistory(doingproject1);                                                        //自动向历史项目表中添加信息
            queryWrapper.clear();
        }
        return Result.success("删除成功！");
    }

    @Override
    public Result update(Customer customer) {
        

        return null;
    }

    @Override
    public Result selectone(Integer pro_id,Integer staff_id) {
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("pro_id",pro_id);
        queryWrapper.eq("staff_id",staff_id);
        queryWrapper.eq("deleted",0);
        Doingproject doingproject = doingprojectMapper.selectOne(queryWrapper);
        if (doingproject != null) {
            return Result.success(doingproject);
        }
        return Result.fail(ErrorCode.PARAMS_ERROR.getCode(),"未查询到该信息！");
    }

    @Override
    public Result selectcall(Integer pro_id) {
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("pro_id",pro_id);
        queryWrapper.eq("deleted",0);
        List<Doingproject> list =(List<Doingproject>) doingprojectMapper.selectList(queryWrapper);
        if (list.size() > 0) {
            return Result.success(list);
        }
        return Result.fail(ErrorCode.PARAMS_ERROR.getCode(),"未查询到该项目信息！");
    }

    @Override
    public Result opinion(Integer pro_id, Integer staff_id, String opinion) {
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("pro_id",pro_id);
        queryWrapper.eq("staff_id",staff_id);
        queryWrapper.eq("deleted",0);
        Doingproject doingproject = doingprojectMapper.selectOne(queryWrapper);
        if(doingproject == null || doingproject.getDeleted() == 1){
            return Result.fail(ErrorCode.PARAMS_ERROR.getCode(), "未查询到该项目信息");
        }
        doingproject.setFeedBack(opinion);
        if (doingprojectMapper.updateById(doingproject) > 0) {
            return Result.success("意见反馈成功！");
        }
        return Result.fail(ErrorCode.PARAMS_ERROR.getCode(), "意见反馈失败！");
    }
}
